#include "timeconfig.h"

extern unsigned long timercount ;

void TIM_NVIC_Config(FunctionalState NewState)
{
  NVIC_InitTypeDef NVIC_InitStructure;

  NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
  NVIC_InitStructure.NVIC_IRQChannelCmd = NewState;

  NVIC_Init(&NVIC_InitStructure);
}

void TIMConfig(void)
{
	TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
	
  TIM_TimeBaseStructure.TIM_Period = 65535-1;
  TIM_TimeBaseStructure.TIM_Prescaler = 72-1;
  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
  TIM_TimeBaseStructure.TIM_RepetitionCounter = 0x0000;

  TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
 
  TIM_ClearITPendingBit(TIM2,TIM_IT_Update);
  TIM_ITConfig(TIM2, TIM_IT_Update,ENABLE);

 // TIM_Cmd(TIM2, ENABLE);
  
}

void TimeInit(void)
{
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
	TIM_NVIC_Config(ENABLE);
	TIMConfig();
}


void TIM2_IRQHandler(void)
{  	
	if(TIM_GetITStatus(TIM2,TIM_IT_Update))
	{   
		TIM_ClearITPendingBit(TIM2,TIM_IT_Update);
		timercount++;	
	}
}
	
